<!-- 命令集合测试页面 -->
<template>
	<view style="width: 100%;">
		设备ID(deviceId):
		<input placeholder="设备ID" v-model.trim="deviceId" @blur="doChange()"></input>
		管理者手机:
		<input placeholder="手机" v-model.trim="mob"></input>
		管理者密码:
		<input placeholder="密码" v-model.trim="pwd"></input>
		门锁初始密码:
		<input placeholder="门锁初始密码" v-model.trim="defaultLockPwd"></input>
		数字密码:
		<input placeholder="数字密码" v-model.trim="numPwd"></input>
		<br>
		蓝牙密码:
		<input placeholder="蓝牙密码" v-model.trim="blePwd"></input>
		<button @click="cmd21">21校正时间</button>
		<button @click="cmd26">26恢复出厂设置</button>
		<button @click="cmd23">23配置管理员命令</button>
		<button @click="cmd63Num">63配置数字密码命令</button>
		<button @click="cmd63DelNum">63删除数字密码命令</button>
		<button @click="cmd63Ble">63配置蓝牙密码命令</button>
		<button @click="cmd82">82wifi配置</button>
		<button @click="cmd41Num">41数字开锁</button>
		<button @click="cmd41Ble">41蓝牙开锁</button>
		<button @click="cmd29A">29A蓝牙开锁</button>
		<button @click="cmdBleNH">南湖蓝牙开锁</button>		
	</view>
</template>

<script>
	import util from '@/utils/util.js';
	import bleUtils from '@/utils/BleUtils.js';
	import {
		cmd21,
		cmd23,
		cmd26,
		cmd29,
		cmd41,
		cmd63,
		cmd82
	} from '@/utils/cmd/index.js';
import { getNHunlockManage, getNHunlockReply } from '../../api/userInfo';

	export default {
		data() {
			return {
				mob: '15006110118',
				pwd: '123456',
				deviceId: '00110520282307007A',
				bleName: '',
				defaultLockPwd: '',
				formatLockMac: '',
				blePwd: 'qwertyuiop',
				numPwd: '123456',
				wifiName: 'Xiaomi',
				wifiPwd: '88888888',
				receiveBleStr: '', // 收到的蓝牙锁回复信息
				recordId:'',
				replyMessage:'',
				cmdLen:0,
			}
		},
		async onLoad() {
			const lockMac = this.deviceId.substring(6);
			this.bleName = '0905' + lockMac;
			this.defaultLockPwd = lockMac.substr(0, 6);
			this.formatLockMac = util.strInsert(lockMac, 2);
			try {
				if (!this.$sysInfo.locationEnabled) {
					util.alert('请先开启手机GPS定位服务!');
					util.hideLoading();
					return;
				}
				if (!this.$sysInfo.locationAuthorized) {
					// 让用户授权开启定位服务 为了让老版本的安卓手机能使用蓝牙
					await bleUtils.doAuthorize('scope.userLocation')
				}
				// 打开蓝牙扫描设备
				await bleUtils.openBluetoothAdapter();
			} catch (err) {
				util.alert('请先蓝牙适配器');
				return;
			}
		},
		onUnload() {
			console.log('onUnload');
			// 关闭蓝牙连接
			bleUtils.closeBLEConnection();
			// 关闭蓝牙适配器
			bleUtils.closeBluetoothAdapter()
		},
		methods: {
			async cmd21() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd21.generate();
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd21 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async cmd26() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd26.generate(this.mob, this.pwd);
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd26 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async cmd29A() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd29.generate(this.deviceId);
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd23 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async cmd23() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd23.generate(this.deviceId, this.mob, this.pwd, this.defaultLockPwd);
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd23 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async cmd63Num() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd63.generate(this.mob, this.pwd, this.mob, this.pwd, this.numPwd,
						'07'); // 04新增蓝牙 05修改蓝牙密码 06删除蓝牙密码
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd63 设置数字密码 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async cmd63DelNum() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd63.generate(this.mob, this.pwd, this.mob, this.pwd, this.numPwd,
						'09'); // 04新增蓝牙 05修改蓝牙密码 06删除蓝牙密码
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd63 设置数字密码 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async cmd63Ble() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd63.generate(this.mob, this.pwd, this.mob, this.pwd, this.blePwd,
						'04'); // 04新增蓝牙 05修改蓝牙密码 06删除蓝牙密码
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd63 设置蓝牙密码 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async cmd82() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd82.generate(this.mob, this.pwd, this.wifiName, this.wifiPwd);
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd82 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}

			},
			async cmd41Num() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd41.generate(this.mob, this.pwd, this.numPwd, '01');
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd41 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async cmd41Ble() {
				try {
					await bleUtils.waitBleConnAndNotify(this.bleName, this.formatLockMac);
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					const cmdMsg = cmd41.generate(this.mob, this.pwd, this.blePwd, '02');
					console.log('send data--->', cmdMsg);
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmd41 err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			
			// 获取南湖蓝牙开锁命令返回结果
			async getNHcmdReply() {
				console.log('getNHcmdReply:')
				let ret = "";
				let params = {
					"recordId":this.recordId,
					"replyMessage":this.replyMessage.toUpperCase(),
				}
				console.log(params)
				let res = await getNHunlockReply(params);
				console.log('getNHunlockReply 返回：',res)
				if (res.statecode === '0') {
					Util.toast(res.statemsg);
				} else {					
					Util.toast(res.statemsg);
				}
			},
			
			// 获取南湖蓝牙开锁命令
			async getNHcmd() {
				let ret = "";
				let params = {
					"isOpen":true,
					"lockNo":"4769476354982101719",
					"mobile":"15298396998"
				}
				let res = await getNHunlockManage(params);
				console.log(res)
				if (res.statecode === '0') {
					ret = res.data.taskMessage;
					this.recordId = res.data.recordId;
				} else {					
					Util.toast(res.statemsg);
				}
				return ret;
			},
						
			async cmdBleNH() {
				try {	
					await bleUtils.waitBleConnAndNotify("CCA12157B857", "CC:A1:21:57:B8:57");
					// 监听当特征值发生变化的回调
					bleUtils.onBLECharacteristicValueChange((res) => {
						this.receiveBleData(res)
					});
					// const cmdMsg = cmd41.generate(this.mob, this.pwd, this.blePwd, '02');
					// console.log('send data--->', cmdMsg);
					// const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);	
					let cmdMsg = await this.getNHcmd();
					console.log(cmdMsg);
					// const cmd_buffer = cmdMsg;
					const cmd_buffer = bleUtils.myStringtoHex(cmdMsg);
					// const cmd_buffer = "55AA003406000000000001CCA12157B8571FB4BC816B30DFCBB21AE05F5541C07F00000102000000E6018D7777770004E82D65827B16DAFD";					
					console.log('send data--->', cmd_buffer);
					// 发送命令
					this.sendPacket(cmd_buffer);
				} catch (err) {
					console.log('cmdNH err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			async receiveBleData(res) {
				try {
					console.log('receive  ble data--->', res);
					
					// this.replyMessage = res.toUpperCase();
					const reply = res.substr(0,4);
					if (reply.toUpperCase()  == '55AA'){
						this.cmdLen = parseInt(res.substr(4,4),16)*2+8;
						this.replyMessage = res;
						// await this.getNHcmdReply();
						console.log('cmdLen',this.cmdLen);
						console.log('this.replyMessage.length  AAA ',this.replyMessage.length);
						console.log('this.replyMessage AAA',this.replyMessage);
					}else {
						console.log('this.replyMessage.length  BBB ',this.replyMessage.length);
						console.log('this.replyMessage BBB',this.replyMessage);
						if (this.replyMessage.length < this.cmdLen){
							this.replyMessage = this.replyMessage + res;
							console.log('this.replyMessage.length  CCC ',this.replyMessage.length);
							console.log('this.replyMessage CCC',this.replyMessage);
							if (this.replyMessage.length == this.cmdLen){
								await this.getNHcmdReply();	
							}
						}
					}					
					const data = res.substring(24, res.length - 2);
					const cmdWord = data.substr(0, 2);
					const resCode = data.substr(2, 2);
					console.log('receive pack data--->', data);
					console.log('receive pack cmdWord--->', cmdWord);
					console.log('receive pack resCode--->', resCode);				
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				} catch (err) {
					console.log('receiveBleData err-->', err)
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			// 发送数据包给蓝牙设备
			async sendPacket(cmdData) {
				try {
					console.log('sendPacket:cmdData-------',cmdData)
					const unit8Array_data = new Uint8Array(cmdData);
					const data = unit8Array_data.buffer;
					console.log('sendPacket:data--------',data)
					await bleUtils.writeBLECharacteristicValue(data);
				} catch (err) {
					util.infotip(err.errCode);
					// 关闭蓝牙连接
					bleUtils.closeBLEConnection();
				}
			},
			doChange() {
				const lockMac = this.deviceId.substring(6);
				this.bleName = '0905' + lockMac;
				this.defaultLockPwd = lockMac.substr(0, 6);
				this.formatLockMac = util.strInsert(lockMac, 2);
			}
		}
	}
</script>

<style lang="scss" scoped>
	button {
		margin-top: 10px;
	}

	input {
		flex: 1;
		color: #1F2642;
		height: 35px;
		border: 1px solid #c3c3c3;
		padding: 0px 10px;
		margin-top: 5px;
	}
</style>